Completed
Push — master ( cdc981...b2a603 )
by greg
01:49
created

post-upload.js ➔ route   A

Complexity

Conditions 3
Paths 61

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 61
dl 0
loc 52
rs 9.4929
nop 3

1 Function

Rating   Name   Duplication   Size   Complexity  
C post-upload.js ➔ ... ➔ req.busboy.file 0 40 12

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import path from 'path'
2
import fse from 'fs-extra'
3
import mkdirp from 'mkdirp'
4
import limax from 'limax'
5
6
import {
7
  config,
8
  abeExtend
9
} from '../../cli'
10
11
var route = function(req, res, next){
12
  abeExtend.hooks.instance.trigger('beforeRoute', req, res, next)
13
  if(typeof res._header !== 'undefined' && res._header !== null) return
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
14
15
  var resp = {success: 1}
16
  var filePath
17
  var folderWebPath = '/' + config.upload.image
18
  folderWebPath = abeExtend.hooks.instance.trigger('beforeSaveImage', folderWebPath, req)
19
  var folderFilePath = path.join(config.root, config.publish.url, folderWebPath)
20
  mkdirp.sync(folderFilePath)
21
  req.pipe(req.busboy)
22
  req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
23
    var ext = path.extname(filename).toLowerCase()
24
    var filenameNoExt = path.basename(filename,ext)
25
    var randID = '-' + (((1+Math.random())*0x100000)|0).toString(16).substring(2)
26
    var slug = limax(filenameNoExt, {separateNumbers: false}) + randID + ext
27
    var hasSentHeader = false
28
    
29
    filePath = path.join(folderFilePath, slug)
30
    resp['filePath'] = path.join(folderWebPath, slug)
31
32
    var returnErr = function (msg) {
33
      hasSentHeader = true
34
      file.resume()
35
      res.set('Content-Type', 'application/json')
36
      res.send(JSON.stringify({error: 1, response: msg}))
37
    }
38
39
    file.on('limit', function() {
40
      returnErr('file is too big')
41
    })
42
43
    if (mimetype !== 'image/gif' && mimetype !== 'image/jpeg' && mimetype !== 'image/png' && mimetype !== 'image/svg+xml' && mimetype !== 'video/mp4') {
44
      returnErr('unauthorized file')
45
    } else if (ext !== '.gif' && ext !== '.jpg' && ext !== '.jpeg' && ext !== '.png' && ext !== '.svg' && ext !== '.mp4') {
46
      returnErr('not a valid asset')
47
    }
48
49
    var fstream = fse.createWriteStream(filePath)
50
51
    fstream.on('finish', function() {
52
      if(hasSentHeader) return
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
53
      if(/\.(jpg|png|gif|svg)/.test(filePath)){
54
        resp = abeExtend.hooks.instance.trigger('afterSaveImage', resp, req)
55
      }
56
      res.set('Content-Type', 'application/json')
57
      res.send(JSON.stringify(resp))
58
    })
59
60
    file.pipe(fstream)
61
  })
62
}
63
64
export default route